<html>
	<head>
		<title>The Real Franker!</title>
		<script type="text/javascript">
			var GOOGLE_API_KEY_DEFAULT    = "INSERT-YOUR-KEY";
			var MICROSOFT_API_KEY_DEFAULT = "INSERT-YOUR-KEY";
			var GOOGLE_URL_TEMPLATE = "https://www.googleapis.com/language/translate/v2?callback=translateCallBack&key=%APIKEY%&q=%TEXT%&target=%DST%&source=%SRC%";
			var MICROSOFT_URL_TEMPLATE = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?oncomplete=translateCallBack&contentType=text/plain&appId=%APIKEY%&text=%TEXT%&to=%DST%&from=%SRC%";
			var MICROSOFT_URL_LANGS_TEMPLATE = "http://api.microsofttranslator.com/V2/Ajax.svc/GetLanguagesForTranslate?oncomplete=languagesCallBack&appId=%APIKEY%"
		</script>
		<script type="text/javascript">
			if (!localStorage["langSrc"]) { localStorage["langSrc"] = "auto" };
			if (!localStorage["langDst"]) { localStorage["langDst"] = "en" };
			
			// handling "BING" as it was used before, in 1.2.2; this _additional_ check can be removed in version > 1.3.0
			if ((!localStorage["engine"])||(localStorage["engine"] == "BING")) { localStorage["engine"] = "MICROSOFT" };
			//if (!localStorage["google_api_key"]) { localStorage["google_api_key"] = GOOGLE_API_KEY_DEFAULT };
			//if (!localStorage["microsoft_api_key"]) { localStorage["microsoft_api_key"] = MICROSOFT_API_KEY_DEFAULT };
			
			if (!localStorage["shortcutFrankate"]) { localStorage["shortcutFrankate"] = "Ctrl+Alt+F" };
			if (!localStorage["shortcutClean"]) { localStorage["shortcutClean"] = "Ctrl+Alt+C" };
			if (!localStorage["style"]) { localStorage["style"] = "color: #292;" };
			if (!localStorage["inject_before"]) { localStorage["inject_before"] = "false" };
			if (!localStorage["inject_brackets"]) { localStorage["inject_brackets"] = "true" };
			
	    </script>
		<script type="text/javascript">
			var frankerPort;
			function translate(text) {
				var escapedText = encodeURIComponent(text).replace(/%22/g, '%26quot;');
				
				var langSrc = localStorage["langSrc"];
				if (langSrc == "auto") {
					langSrc = "";
				}
				var langDst = localStorage["langDst"];
				
				var engine = localStorage["engine"];

				var url = "";
				if (engine == "GOOGLE") {
					var apiKey = localStorage["google_api_key"];
					if (!apiKey || apiKey == "") {
						alert("Franker error: Google Translate requires API key.\n\nPlease, set it at the Franker extension options page.");
						frankerPort.postMessage({name:"stop", message:""});
						return;
					}
					url = GOOGLE_URL_TEMPLATE.replace("%APIKEY%", apiKey);
					url = url.replace("%TEXT%", escapedText);
					url = (langSrc != "") ? url.replace("%SRC%", langSrc) : url.replace("&source=%SRC%", "");
					url = url.replace("%DST%", langDst);
				} else if (engine == "MICROSOFT") {
					var apiKey = localStorage["microsoft_api_key"];
					if (!apiKey || apiKey == "") {
						apiKey = MICROSOFT_API_KEY_DEFAULT;
					}
					if (langSrc == "zh-CN") { langSrc = "zh-CHS" }
					if (langSrc == "zh-TW") { langSrc = "zh-CHT" }
					if (langDst == "zh-CN") { langDst = "zh-CHS" }
					if (langDst == "zh-TW") { langDst = "zh-CHT" }
					if ((langSrc != "" && !(langSrc in microsoftLanguages)) || !(langDst in microsoftLanguages)) {
						alert("Franker error: Microsoft Translator does not support this language pair: "+langSrc+"-"+langDst+".\n\nSupported languages: "+microsoftLanguagesArray+".\n\nPlease, adjust language settings at the Franker extension options page.");
						frankerPort.postMessage({name:"stop", message:""});
						return;
					}
					url = MICROSOFT_URL_TEMPLATE.replace("%APIKEY%", apiKey);
					url = url.replace("%TEXT%", escapedText);
					url = url.replace("%SRC%", langSrc);
					url = url.replace("%DST%", langDst);
				} else {
					return;
				}
				//alert(url);
				injectScript("frankerScript", url);
			}
			function translateCallBack(result) {
				var translation = "";
				var engine = localStorage["engine"];
				if (engine == "GOOGLE") {
					translation = result.data.translations[0].translatedText;
				} else if (engine == "MICROSOFT") {
					translation = result;
				}
				frankerPort.postMessage({name:"frankateSelectionResponse", message:translation});
			}
			function respondToMessage(theMessageEvent) {
		    	if(theMessageEvent.name === "frankateSelectionRequest") {
					translate(theMessageEvent.message);
				} else if (theMessageEvent.name === "shortcutFrankateSelectionRequest") {
					frankerPort.postMessage({name:"shortcutFrankateSelectionValue", message:localStorage["shortcutFrankate"]});
				} else if (theMessageEvent.name === "shortcutFrankateCleanRequest") {
					frankerPort.postMessage({name:"shortcutFrankateCleanValue", message:localStorage["shortcutClean"]});
				} else if (theMessageEvent.name === "styleDestinationRequest") {
					frankerPort.postMessage({name:"styleDestinationValue", message:localStorage["style"]});
				} else if (theMessageEvent.name === "injectBeforeRequest") {
					frankerPort.postMessage({name:"injectBeforeValue", message:localStorage["inject_before"]});
				} else if (theMessageEvent.name === "injectBracketsRequest") {
					frankerPort.postMessage({name:"injectBracketsValue", message:localStorage["inject_brackets"]});
				}
			}
			function injectScript(id, url){
				var s = document.getElementById(id);
				if (s != null) {
					document.getElementsByTagName("head")[0].removeChild(s);
				}
				s = document.createElement("script");
				s.id = id;
				s.type = "text/javascript";
				s.src = url;
				document.getElementsByTagName("head")[0].appendChild(s);
			}
			
			chrome.extension.onConnect.addListener(function(port) {
			  frankerPort = port;
			  console.assert(port.name == "Franker");
			  port.onMessage.addListener(respondToMessage);
			});
		</script>
		<script type="text/javascript">
			function frankerContextOnClick(info, tab) {
				chrome.tabs.executeScript(tab.id,{code:"frankerInjectFrankate();",allFrames:true});
			}
			chrome.contextMenus.create({"title": "Frankate", "contexts":["selection"],
                                       "onclick": frankerContextOnClick});
		</script>
		<script type="text/javascript">
			var microsoftLanguages = {};
			var microsoftLanguagesArray = [];
			function languagesCallBack(result) {
				microsoftLanguagesArray = result;
				for(var i=0;i<result.length;i++) {
					microsoftLanguages[result[i]]='';
				}
			}
			injectScript("microsoftLanguagesScript", MICROSOFT_URL_LANGS_TEMPLATE.replace("%APIKEY%", MICROSOFT_API_KEY_DEFAULT));
		</script>
	</head>
	<body>
	</body>
</html>
